package com.cjc.pinyin4j;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.cjc.pinyin4j.lib.AssortPinyinList;
import com.cjc.pinyin4j.lib.LanguageComparator_CN;
import com.cjc.pinyin4j.lib.LanguageComparator_EN;

/**
 * 
 ***************************************************** 
 * <hr>
 * <dt><span class="strong">类功能简介:</span></dt>
 * <dd>Coffee</dd>
 * <dt><span class="strong">创建时间:</span></dt>
 * <dd>2015-4-3 下午1:45:44</dd>
 * <dt><span class="strong">公司:</span></dt>
 * <dd>拼音Adapter</dd>
 * 
 * @author aa1000777 - Email:aa1000777@qq.com
 ***************************************************** 
 */
public class PinyinAdapter extends BaseExpandableListAdapter {
	private final String MY_CN = "CN";
	private String language="CN";
	// Pinyin排序字段
	public AssortPinyinList assort;
	// 用于记录Eid
	public Map<String, String> Pinyinid = new HashMap<String, String>();
	// 用于记录PinyinName
	public Map<String, String> PinyinName = new HashMap<String, String>();
	// 中文排序
	private LanguageComparator_CN cnSort = new LanguageComparator_CN();
	private LanguageComparator_EN enSort = new LanguageComparator_EN();
	private List<Pinyin> data = new ArrayList<Pinyin>();
	private LayoutInflater inflater;

	public PinyinAdapter(Context context, List<Pinyin> data, String language) {
		inflater = LayoutInflater.from(context);
		this.data = data;
		this.language=language;
		Pinyinid.clear();
		Global.Pinyinid.clear();
		PinyinName.clear();
		Global.PinyinName.clear();
		assort = new AssortPinyinList();
		// 排序
		if (!MY_CN.equals(language)) {
			/* 英文排序 */
			enSort();
		} else {
			/* 中文排序 */
			cnSort();
		}
		Global.Pinyinid=Pinyinid;
		Global.PinyinName=PinyinName;
		Global.assort=assort;
	}
	public void updateListView(List<Pinyin> data,String language) {
		this.data = data;
		this.language=language;
		Pinyinid.clear();
		Global.Pinyinid.clear();
		PinyinName.clear();
		Global.PinyinName.clear();
		assort = new AssortPinyinList();
		// 排序
		if (!MY_CN.equals(language)) {
			enSort();
		} else {
			cnSort();
		}
		Global.Pinyinid=Pinyinid;
		Global.PinyinName=PinyinName;
		Global.assort=assort;
		notifyDataSetChanged();
	}
	/**
	 * 
	 ***************************************************** 
	 * 方法简介: 英文排序
	 ***************************************************** 
	 */
	private void enSort() {
		// TODO Auto-generated method stub
		// 分类
		int size = data.size();
		for (int i = 0; i < size; i++) {
			assort.getHashList().add(data.get(i).PinyinEn);
			this.Pinyinid.put(data.get(i).PinyinEn, data.get(i).Id);
			this.PinyinName.put(data.get(i).PinyinEn, data.get(i).PinyinCn);
		}
		assort.getHashList().sortKeyComparator(enSort);
		size = assort.getHashList().size();
		for (int i = 0; i < size; i++) {
			Collections.sort((assort.getHashList().getValueListIndex(i)), enSort);
		}
	}

	/**
	 * 
	 ***************************************************** 
	 * 方法简介: 中文排序
	 ***************************************************** 
	 */
	private void cnSort() {
		int size = data.size();
		// 分类
		for (int i = 0; i < size; i++) {
			assort.getHashList().add(data.get(i).PinyinCn);
			this.Pinyinid.put(data.get(i).PinyinCn, data.get(i).Id);
			this.PinyinName.put(data.get(i).PinyinCn, data.get(i).PinyinEn);
		}
		assort.getHashList().sortKeyComparator(cnSort);
		size = assort.getHashList().size();
		for (int i = 0; i < size; i++) {
			Collections.sort((assort.getHashList().getValueListIndex(i)), cnSort);
		}
	}

	public Object getChild(int group, int child) {
		// TODO Auto-generated method stub
		return assort.getHashList().getValueIndex(group, child);
	}

	public long getChildId(int group, int child) {
		// TODO Auto-generated method stub
		return child;
	}

	@Override
	public View getChildView(int group, int child, boolean arg2, View convertView, ViewGroup arg4) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.fragment_pinyin_item, null);
			holder.tv_item_name_cn = (TextView) convertView.findViewById(R.id.tv_item_name_cn);
			holder.tv_item_name_en = (TextView) convertView.findViewById(R.id.tv_item_name_en);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		// 赋值
		if (MY_CN.equals(this.language)) {
			holder.tv_item_name_cn.setText(assort.getHashList().getValueIndex(group, child));
			holder.tv_item_name_en.setText(PinyinName.get(assort.getHashList().getValueIndex(group, child).toString()));
		}else{
			holder.tv_item_name_cn.setText(PinyinName.get(assort.getHashList().getValueIndex(group, child).toString()));
			holder.tv_item_name_en.setText(assort.getHashList().getValueIndex(group, child));
		}
		return convertView;
	}

	class ViewHolder {
		TextView tv_item_name_cn;
		TextView tv_item_name_en;
	}

	public int getChildrenCount(int group) {
		// TODO Auto-generated method stub
		return assort.getHashList().getValueListIndex(group).size();
	}

	public Object getGroup(int group) {
		// TODO Auto-generated method stub
		return assort.getHashList().getValueListIndex(group);
	}

	public int getGroupCount() {
		// TODO Auto-generated method stub
		return assort.getHashList().size();
	}

	public long getGroupId(int group) {
		// TODO Auto-generated method stub
		return group;
	}

	public View getGroupView(int group, boolean arg1, View contentView, ViewGroup arg3) {
		if (contentView == null) {
			contentView = inflater.inflate(R.layout.fragment_pinyin_list_group_item, null);
			contentView.setClickable(true);
		}
		TextView textView = (TextView) contentView.findViewById(R.id.name);
		textView.setText(assort.getFirstChar(assort.getHashList().getValueIndex(group, 0)));
		// 禁止伸展
		return contentView;
	}

	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	public boolean isChildSelectable(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return true;
	}

	public AssortPinyinList getAssort() {
		return assort;
	}
}
